<template>
    <!-- 人员管理-黑名单管理 -->
    <div class="empBlacklist">
        <div class="top">
            <div class="search">
                <div class="list">
                        <Input search enter-button placeholder="人员姓名搜索"  style="width:250px"  @on-search="onSearch" @on-enter="onSearch" v-model="keyword" clearable />
                    <input type="password" autocomplete="new-password" style="display: none" />
                </div>
                
            </div>
            <div class="down">
                <el-button type="primary" @click="updateRow" size="small" plain :disabled="!btnList.includes('emp/blacklist/add')">添加黑名单人员</el-button>
            </div>
        </div>
        <div class="bottom">
            <div class="table">
                <el-table :data="list" style="width: 100%" :height="tableHeight" :stripe='true'>
                    <el-table-column fixed prop="empName" label="姓名"></el-table-column>
                    <el-table-column prop="idCardNo" label="身份证号"></el-table-column>
                    <el-table-column prop="time" label="添加时间"></el-table-column>
                    <el-table-column prop="userName" label="添加账号"></el-table-column>
                    <el-table-column prop="reason" label="加入黑名单原因"></el-table-column>
                    <el-table-column prop="groupName" label="班组"></el-table-column>
                    <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button  @click.native.prevent="DelRow(scope.$index, list)" type="text" style="color:red" size="small" :disabled="!btnList.includes('emp/blacklist/delete')">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="clearfix" v-show="list.length>0">
                <div class="float-right">
                    <Page v-if="totalCount" style="margin: 20px 0;text-align: center;" :current.sync="current" :total="totalCount" :page-size="pageSize" show-elevator show-total show-sizer @on-change="pageChange" @on-page-size-change="pageSizeChange"></Page>
                </div>
            </div>
        </div>
                
        <Modal v-model="empModal" title="添加人员黑名单" width="660px;" :mask-closable="false">
            <h2>添加人员</h2>
            <el-transfer v-model="emplist" :data="msgList" filterable :filter-method="filterMethod" ref="myTransfer" filter-placeholder="请输入人员名称"   :titles="['所有人员', '已选人员']"></el-transfer>
            <h2 style="margin-top:20px">添加原因</h2>
            <el-input type="textarea" autosize placeholder="请输入添加黑名单原因" v-model="reason" maxlength="20" show-word-limit> </el-input>
            <div slot="footer">
                <el-button @click="cancel(0)" type="info" size="small" plain>取消</el-button>
                <el-button @click="subMitEmp" type="primary" size="small" plain v-preventReClick="1500">确定</el-button>
            </div>
        </Modal>
        <Modal v-model="deteleModal" title="删除" width="360px;" :mask-closable="false">
            <div style="margin:10px">确定要删除这条数据吗？</div>
            <div slot="footer">
                <el-button @click="cancel(1)" type="info" size="small" plain>取消</el-button>
                <el-button @click="detele" type="primary" size="small" plain v-preventReClick="1500">确定</el-button>
            </div>
        </Modal>
    </div>
</template>

 <script>

import { getEmpBackList,getEmpBlackList,getEmpBackAdd,getEmpBackDel } from "@/api/api.js";
export default {
    data() {
        return {
            pageNum: 1,
            pageSize: 10,
            totalCount: "",
            current: 1,
            list: [],
            keyword:"",
            deptId: "",
            emplist:[],
            msgList:[],
            btnList:[],
            empModal:false,
            reason:"",
            deteleModal:false,
            deteleId:"",
            tableHeight:window.innerHeight - 355 ,

        };
    },
    computed: {
        porjectObject() {
            return this.$store.state.porjectObject;
        }
    },
    watch: {
        porjectObject(val, oldVal) {
            if (val.deptId != oldVal.deptId) {
                this.deptId = val.deptId;
                this.current = 1;
                this.pageNum = 1;
                this.keyword = "";
                this.getlist();
            }
        }
    },
    methods: {
        getlist() {
            let params = {
                pageSize: this.pageSize,
                pageNum: this.pageNum,
                keyword: this.keyword,
                deptId: this.deptId,
            };
            getEmpBackList(params)
                .then(res => {
                    if (res.code == 200) {
                        this.list = res.data.list;
                        this.totalCount = res.data.total;
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },
        pageChange(index) {
            this.pageNum = index;
            this.getlist();
        },
        pageSizeChange(index){
            this.pageSize = index;
            this.getlist();
        },
        onSearch() {
            this.current = 1;
            this.pageNum = 1;
            this.getlist();
        },
        filterMethod(query, item) {
            return ( item.empName.indexOf(query) > -1 );
        },
        updateRow(){
            this.empModal = true;
            this.msgList = [];
            this.emplist = [];
            this.reason = "";
            this.$refs.myTransfer.$children["0"]._data.query = '';
            // 清空右边搜索
            this.$refs.myTransfer.$children["3"]._data.query = '';
            getEmpBlackList({
                deptId:this.deptId
            }).then(res=>{
                if(res.code == 200) {
                    for (let i = 0 ; i<res.data.list.length; i++) {
                        this.msgList.push({
                            key: i.toString(),
                            label:  res.data.list[i].empName + (res.data.list[i].idCardNo || ''),
                            deptId: res.data.list[i].deptId,
                            empId: res.data.list[i].empId,
                            empName: res.data.list[i].empName,
                            idCardNo: res.data.list[i].idCardNo,
                            deptName:res.data.list[i].deptName,
                        });
                    }
                } else {
                   this.$Message.warning(res.msg); 
                }
            }).catch(error=>{
                console.log(error)
            })
        },
        subMitEmp(){
            let arr = [];
            for(let i = 0; i<this.msgList.length; i++ ) {
               for(let j = 0; j< this.emplist.length; j++) {
                   if(this.msgList[i].key == this.emplist[j]) {
                       arr.push({
                            deptId: this.msgList[i].deptId,
                            empId: this.msgList[i].empId,
                            empName: this.msgList[i].empName,
                            idCardNo: this.msgList[i].idCardNo,
                            deptName:this.msgList[i].deptName, 
                       })
                   }
               }
            }
            if(arr.length == 0){
                this.$Modal.warning({
                    title: "提示",
                    content: "请选择需要添加的人员"
                });
                return; 
            }
            if(this.reason == ""){
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请填写加入黑名单原因"
                });
                return;
            }
            let params = {
                empList: arr,
                reason:this.reason
            }
            getEmpBackAdd(params).then(res=>{
                if(res.code == 200) {
                    this.$Message.success("操作成功");
                    this.keyword = "";
                    this.empModal = false;
                    this.pageNum = 1;
                    this.current = 1;
                    this.getlist();
                } else {
                   this.$Message.warning(res.msg); 
                }
            }).catch(error =>{
                cosnole.log(error)
            })
        },
        DelRow(index,rows){
          this.deteleId =rows[index].id;
          this.deteleModal = true
        },
        detele(){
            getEmpBackDel(this.deteleId).then(res=>{
                if(res.code == 200) {
                    this.$Message.success("删除成功");
                    this.keyword = "";
                    this.deteleModal = false;
                    this.pageNum = 1;
                    this.current = 1;
                    this.getlist(); 
                } else {
                    this.$Message.warning(res.msg);  
                }
            }).catch(error=>{
               console.log(error)
            })
        },
        cancel(type){
           if(type == 0){
               this.empModal =false;
           } else if(type == 1) {
               this.deteleModal = false;
           }
        }
    },
    mounted() {
        this.deptId = this.$store.state.porjectObject.deptId;
        this.btnList = JSON.parse(localStorage.getItem("arr"));
        this.getlist();
    }
};
</script>
 <style lang="less" scoped>
// @import "./../../assets/style/emp_common.less";
@import "./../../assets/style/themeBase.less";
.empBlacklist {
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
    background: #f5f5f5;
    .top {
        width: 100%;
        height: 72px;
        background: #fff;
        border-radius: 10px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .search {
            width: auto;
            margin-left: 20px;
            padding-top: 20px;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            .list {
                display: flex;
                justify-content: flex-start;
                align-items: center;
                padding-bottom: 20px;
                margin-right: 20px;
                span {
                    width: 70px;
                }
            }
        }
        .down {
            width: auto;
            margin-right: 20px;
        }
    }
    .bottom {
        width: 100%;
        padding: 20px;
        margin-top: 10px;
        box-sizing: border-box;
        height: calc(100% - 82px);
        background: #fff;
        border-radius: 10px;
        .down {
            width: 98%;
            margin: 0 auto;
            margin-top: 20px;
        }
        .msg {
            margin-bottom: 20px;
            display: flex;
            flex-wrap: wrap;
            .list {
                display: flex;
                justify-content: flex-start;
                align-items: center;
                margin-right: 24px;
                p {
                    margin-right: 10px;
                    font-size: 16px;
                    color: #000;
                    &:nth-child(2) {
                        color: red;
                    }
                }
            }
        }
        .updateCorp {
            cursor: pointer;
            margin-left: 5px;
            padding: 2px 4px;
            font-size: 12px;
            font-weight: 400;
            line-height: 1;
            color: #ffffff;
            border-radius: 3px;
            background-color: var(--themeColor);
        }
    }
}
.item {
    display: flex;
    justify-content: flex-start;
    align-items: center;
}

/deep/.ivu-input-group{
    border-radius:20px;
    }
    /deep/.ivu-input-group .ivu-input{
        border-radius: 20px 0 0 20px;
        border-color:#EBF2F8;
        background:#EBF2F8;
    }
    /deep/.ivu-input-group-append{
        border-radius: 0 20px 20px 0;
    }
    /deep/.el-tag.el-tag--success{
        background-color: #E7F8F2;
        border-color: #28D79C;
        color: #3FCF77;
    }
    /deep/.el-tag{
        border-radius: 2px;
    }
    /deep/.ivu-select-selection{
        border-radius: 2px;
    }
</style>
 
 
